﻿Imports System.Windows.Forms

Public Class IIS

    Private _path As String
    Public Property Path() As String
        Get
            Return _path
        End Get
        Set(ByVal value As String)
            _path = value
        End Set
    End Property


    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        _path = String.Empty
        Me.Close()
    End Sub

    Private Sub Dialog1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        FillTreeView()
    End Sub

    Private Sub FillTreeView()
        Dim rootNode As TreeNode
        rootNode = Me.tvwIIS.Nodes.Add("iis", "ROOT")
        'On récupère les éléments de IIS
        Dim iis As New DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/1/Root")
        Dim ent As DirectoryServices.DirectoryEntry
        For Each ent In iis.Children
            rootNode.Nodes.Add(ent.Name, ent.Name)
        Next

        tvwIIS.Sort()
        tvwIIS.ExpandAll()
    End Sub

    Private Sub tvwIIS_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvwIIS.AfterSelect
        If Not tvwIIS.SelectedNode.Text = "ROOT" AndAlso Not tvwIIS.SelectedNode.Text = String.Empty Then
            Dim iis As New DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/1/Root")
            Dim ent As DirectoryServices.DirectoryEntry
            For Each ent In iis.Children
                If ent.Name = tvwIIS.SelectedNode.Text Then
                    _path = ent.Properties("AppRoot").Value.ToString
                End If
            Next
        End If
    End Sub
End Class
